<?php

class Map extends MX_Controller {

	private $_redirect = '/dashboard';
	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->library(array('form_validation', 'core'));
		$this->load->library('form_validation');

		$this->lang->load('referensi/referensi', 'indonesia');
		
		$this->load->helper('cookie');
		$this->load->helper('language');
		
		$this->load->model('map_model');
		$this->load->model('mmaster');
	}
	
	public function index()
	{
		if (!$this->session->userdata('is_logged_in')) {
			redirect('/login/' . $this->core->encode($this->uri->uri_string()));
		}
		$site_nom=3;
		$radius=400;
		$site_nom=isset($_POST['nomsite']) ? $_POST['nomsite'] : 3;
		$radius=isset($_POST['radius']) ? $_POST['radius'] : 400;

		$data_nom = $this->map_model->get_site_by_id($site_nom);
		$nomlat=$data_nom[0]['latitude'];
		$nomlong=$data_nom[0]['longitude'];
		$nomname=$data_nom[0]['site_id_text'];
		$data['petak']=$this->map_model->get_tp_by_radius($site_nom, $radius, $nomlat, $nomlong);
		
		$kandidat=0;
		$tp=1;
		$koord_nomsend = $this->map_model->get_koord_nom_site($site_nom);
		$nomlat=$koord_nomsend['lat'];
		$nomlong=$koord_nomsend['long'];
		$data['radiuss'] = $radius;
		$data['koord_nom_lat'] = $nomlat;
		$data['koord_nom_long'] = $nomlong;
		$data['nomname'] = $nomname;
		$data['list_site_id'] = $this->map_model->get_site_name();
		$data['gambar_marker'] = $this->map_model->get_marker();
	    
		$this->theme->set('page_title', 'PMTS - Map');		
		$this->theme->view('map_view2', $data);
	}	
	
	public function mapnsf()
	{
		if (!$this->session->userdata('is_logged_in')) {
			redirect('/login/' . $this->core->encode($this->uri->uri_string()));
		}
		$site_nom=3;
		//$radius=400;
		$site_nom=isset($_POST['nomsite']) ? $_POST['nomsite'] : 3;
		//$radius=isset($_POST['radius']) ? $_POST['radius'] : 400;
		$data_nom = $this->map_model->get_site_by_id($site_nom);
		$nomlat=$data_nom[0]['latitude'];
		$nomlong=$data_nom[0]['longitude'];
		$nomname=$data_nom[0]['site_id_text'];
		$data['petak']=$this->map_model->get_tp_kandidat_by_siteid($site_nom, $nomlat, $nomlong);
		
		$kandidat=0;
		$tp=1;
		$koord_nomsend = $this->map_model->get_koord_nom_site($site_nom);
	    $nomlat=$koord_nomsend['lat'];
	    $nomlong=$koord_nomsend['long'];
	    
		//$data['radiuss'] = $radius;
		$data['koord_nom_lat'] = $nomlat;
		$data['koord_nom_long'] = $nomlong;
		$data['nomname'] = $nomname;
		$data['list_site_id'] = $this->map_model->get_site_name();
		$data['gambar_marker'] = $this->map_model->get_marker();
		$this->theme->set('page_title', 'PMTS - Map');		
		$this->theme->view('mapnsf_view', $data);
	}	
	
	public function kandidat(){
		$nomor = base64_decode($_GET['nomor']);
		$site_id = base64_decode($_GET['site_id']);
		//die('nomor '.$nomor.' site_id'.$site_id);
		//$this->core->decode($this->uri->segment(3));
		$data = array(
		   'site_id' => $site_id,
		   'site_id_tp' => $nomor ,
		   'created_by' => $this->session->userdata('username') ,
		   'status' => 1
		);
		if($this->map_model->cek_site_kandidat($site_id, $nomor)){
			$error_msg="record already exist";
			$this->kandidat_site($site_id);
			//echo $error_msg;
		}else{
			$error_msg="";
			$update=$this->map_model->update_kandidat($nomor);
			$add=$this->map_model->add_kandidat($data);
			if($update && $add){
				$this->kandidat_site($site_id);
			}else{
				//echo "gagal";
				redirect('referensi/map');
			}	
		}
		

	}	

	public function kandidat_site($site_id){
		if ($this->core->is_ajax()) {
			
			if ($this->input->get('cmd') == 'GET_KANDIDAT') {
			
				$json = $this->map_model->get_site_kandidat($site_id);
				
				echo json_encode($json);
				exit;
			}
			
		}
		
		$this->theme->set('page_title', 'PMTS - Site Info - Kandidat Site');		
		$this->theme->view('kandidat_view3');

	}

	public function kandidat_site_list(){
		$site_id=isset($_GET['nomsite']) ? $_GET['nomsite'] : 3;
		//die("site_id ".$site_id);
		if (!$this->session->userdata('is_logged_in')) {
			redirect('/login/' . $this->core->encode($this->uri->uri_string()));
		}
		$json2 = $this->map_model->get_site_kandidat($site_id);
		//die(print_r($json2));
		if ($this->core->is_ajax()) {
			
			if ($this->input->get('cmd') == 'GET_KANDIDAT') {
				
				if ($site_id) {
					$json = $this->map_model->get_site_kandidat($site_id);
				} else {
					$json = $this->map_model->get_site_kandidat();
				}
				
				echo json_encode($json);
				exit;
			}
			
		}
		
		$data['list_site_id'] = $this->map_model->get_site_name();
		
		$this->theme->set('page_title', 'PMTS - Site Info - Kandidat Site');		
		$this->theme->view('kandidat_site_list_view',$data);

	}

	public function kandidat_filter($site_id=1){
		//1=semua, 2=Belum Kandidat, 3=Kandidat, 4=Disewa
		//$site_id=1;
		//$site_id = $this->input->post('site_id');
		$status = $_GET['status'];//$this->input->post('status');
		$site_idd = $_GET['site_id_filter'];
		//die('>>'.$site_id);
		$data['tower_provider'] = $this->map_model->get_tower_provider_filter($site_idd,$status);
		$data['site_idd']= $site_idd;
		$this->theme->set('page_title', 'PMTS - Site Info - Kandidat Site');		
		$this->theme->view('kandidat_view', $data);

	}

	public function kandidat_site_list_nsf(){
		$site_id=isset($_GET['nomsite']) ? $_GET['nomsite'] : 3;
		//die("site_id ".$site_id);
		if (!$this->session->userdata('is_logged_in')) {
			redirect('/login/' . $this->core->encode($this->uri->uri_string()));
		}
		$json2 = $this->map_model->get_site_kandidat($site_id);
		//die(print_r($json2));
		if ($this->core->is_ajax()) {
			
			if ($this->input->get('cmd') == 'GET_KANDIDAT') {
				
				if ($site_id) {
					$json = $this->map_model->get_site_kandidat($site_id);
				} else {
					$json = $this->map_model->get_site_kandidat();
				}
				
				echo json_encode($json);
				exit;
			}
			
		}
		
		$data['list_site_id'] = $this->map_model->get_site_name();
		
		$this->theme->set('page_title', 'PMTS - Site Info - Kandidat Site');		
		$this->theme->view('kandidat_site_list_nsf_view',$data);

	}

	public function disewa(){
		$nomor = $_GET['nomor'];
		$site_id = $_GET['site_id'];
		$update=$this->map_model->update_disewa($nomor);
		if($update){
		    $this->kandidat_site($site_id);
		}else{
		    //echo "gagal";
		    redirect('referensi/map');
		}

	}	

	function all_site(){
		$data['site_name'] = $this->map_model->get_site_name();
	}

	function detil_tp(){
		$tp_id = $_GET['tp_id'];
		$data['tp_detil'] = $this->map_model->get_tp_by_id($tp_id);
		$this->theme->set('page_title', 'PMTS - Site Info - Kandidat Site - TP Detil');		
		$this->theme->view('detil_tp_view', $data);
	}
	
	function verifikasi_kandidat(){
		$tp_id = $_GET['tp_id'];
		$nomsite_id = $_GET['nomsite_id'];
		$update=$this->map_model->update_status_verified_tp($tp_id,$nomsite_id);	
		if($update){
		    echo "sukses";
			//$this->kandidat_site($site_id);
		}else{
		    echo "gagal";
		    //redirect('referensi/map');
		}
	}

	function pilih_pemenang(){
		$tp_id = $_GET['tp_id'];
		$nomsite_id = $_GET['nomsite_id'];
		$update=$this->map_model->update_status_pemenang_tp($tp_id,$nomsite_id);	
		if($update){
		    echo "sukses";
		}else{
		    echo "gagal";
		}
		
	}

	function topology(){
		$data['sitelink']=$this->map_model->get_site_link();
		//die(print_r($data['sitelink']));
		$this->theme->set('page_title', 'PMTS - Site Info - Topology');		
		$this->theme->view('map_topology',$data);	
	}
}